package com.yishusmt.wechatapi.wechatdev.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.yishusmt.wechatapi.wechatdev.dto.response.ImgAndTextRes;
import org.dom4j.DocumentException;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息体解析传输
 *
 * @author zhangyi
 * @date 2018/8/28 10:42
 */
public class WeChatXmlUtil {

    private static String PREFIX_CDATA = "<![CDATA[";
    private static String SUFFIX_CDATA = "]]>";

    /**
     * 将数据加上格式转化
     */
    static XStream initXStream() {
        return new XStream(new XppDriver() {
            @Override
            public HierarchicalStreamWriter createWriter(Writer out) {
                return new PrettyPrintWriter(out) {
                    @Override
                    protected void writeText(QuickWriter writer, String text) {
                        writer.write(PREFIX_CDATA + text + SUFFIX_CDATA);
                    }
                };
            }
        });
    }
//
//    /**
//     * 纯文本消息
//     *
//     * @param map
//     * @return
//     */
//    public static String objToXml(Map<String, String> map) {
//        String xmlResult = "";
//        StringBuffer sb = new StringBuffer();
//        sb.append("<xml>");
//        for (String key : map.keySet()) {
//            String value = "<![CDATA[" + map.get(key) + "]]>";
//            sb.append("<" + key + ">" + value + "</" + key + ">");
//        }
//        sb.append("</xml>");
//        xmlResult = sb.toString();
//        return xmlResult;
//    }

    /**
     * 发送图文消息图文消息
     *
     * @param object
     * @return
     */
    public static String objToXml(Object object){
        XStream xStream = initXStream();
        xStream.processAnnotations(object.getClass());
        xStream.aliasSystemAttribute(null,"class");
        return xStream.toXML(object).replace("<>","").replace("</>","");
    }

    /**
     * @description 将request转换成map
     * @param request
     * @return Map
     */
    public static Map<String, String> xmlToObj(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        Document doc = null;
        SAXReader saxReader = new SAXReader();
        try {
            ServletInputStream inputStream = request.getInputStream();
            // 将字符串转为XML
            doc = saxReader.read(inputStream);
            // 获取根节点
            Element rootElt = doc.getRootElement();
            // 获取根节点下所有节点
            @SuppressWarnings("unchecked")
            List<Element> list = rootElt.elements();
            for (Element element : list) {
                map.put(element.getName(), element.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
